import java.util.HashSet;
import java.util.LinkedList;
import java.util.concurrent.LinkedBlockingQueue;


public class TestServerComunicator implements ServerComm {
	private LinkedBlockingQueue<ServerSideMessage> listOfMessagesToBeProcessed = new LinkedBlockingQueue<ServerSideMessage>();
	private LinkedList<String> serverCommMethodCalls = new  LinkedList<String>();
	private HashSet<String> connectedClients = new HashSet<String>(); 


	@Override
	public void start() {
		serverCommMethodCalls.addLast("Start method was called.");
	}

	@Override
	public ServerSideMessage getNextMessage() {
		try {
			ServerSideMessage nextMessageOnQueue = listOfMessagesToBeProcessed.take();
			return nextMessageOnQueue;
		} catch (InterruptedException e) {e.printStackTrace();}
		return null;
	}

	@Override
	public boolean hasNextMessage() {		
		if (listOfMessagesToBeProcessed.size() != 0) return true;
		return false;
	}

	@Override
	public void sendBirdsongMessage(String fromNicename, String toNickname, String birdsongMessage) {
		serverCommMethodCalls.addLast("A BirdSongMessage was sent from " + fromNicename + " to " + toNickname + "and it content is " + birdsongMessage + ".");
	}

	@Override
	public void sendNewFollower(String toNickname, String newFollower) {
		serverCommMethodCalls.addLast("A Follow message was sent to " + toNickname + " from " + newFollower + ".");
	}

	@Override
	public void sendNewUnfollow(String toNickname, String unfollower) {
		serverCommMethodCalls.addLast("A Unfollow message was sent to " + toNickname + " from " + unfollower + ".");
	}

	@Override
	public void sendError(String toNickname, String error) {
		serverCommMethodCalls.addLast("A error message was sent to " + toNickname + " with the following content: " + error + ".");

	}

	@Override
	public void sendClientConnected(String toNickname, String connectedNickname) {
		serverCommMethodCalls.add("A client connected message was sent to " + toNickname + " from " + connectedNickname + ".");
	}
	
	@Override
	public void sendClientDisconnected(String toNickname, String disconnectedNickname) {
		serverCommMethodCalls.add("A client disconnected message was sent to " + toNickname + " from " + disconnectedNickname + ".");
	}

	@Override
	public boolean clientIsConnected(String nickname) {
		return connectedClients.contains(nickname);
	}

	@Override
	public void disconnectClient(String nickname) {
		connectedClients.remove(nickname);
	}
	
	public void addToMessageQueue(ServerSideMessage message){
		listOfMessagesToBeProcessed.add(message);
	}

	public void addClient(String nickname){
		connectedClients.add(nickname);
	}

	public HashSet<String> getConnectedClients() {
		return connectedClients;
	}
}
